package com.rbs.controller;

import com.github.pagehelper.PageInfo;
import com.rbs.entity.Category;
import com.rbs.entity.Tea;
import com.rbs.entity.User;
import com.rbs.service.CategoryService;
import com.rbs.service.TeaService;
import com.rbs.service.UserService;
import com.rbs.service.impl.CategoryServiceImpl;
import com.rbs.service.impl.TeaServiceImpl;
import com.rbs.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/grang")
public class GrangController {
    private UserService userService = new UserServiceImpl();
    private TeaService teaService = new TeaServiceImpl();
    private CategoryService categoryService = new CategoryServiceImpl();
    @RequestMapping("/selectUser")
    public String selectUserByUsernameAndPassword(String username, String password){
        User user = userService.selectUserByUsernameAndPassword(username, password);
        if (user != null){
            return "redirect:/grang/selectTeaByPage.do";
        }else {
            return "redirect:/login.jsp";
        }
    }
    @RequestMapping("/selectAllTea")
    public String selectAllTea(Model model){
        List<Tea> teas = teaService.selectAllTeaAndCategory();
        model.addAttribute("teas",teas);
        return "/teaList.jsp";
    }
    @RequestMapping("/insertTea")
    public String insertTea(Tea tea){
        tea.setTeaName(tea.getTeaName());
        tea.setTeaPrice(tea.getTeaPrice());
        tea.setAddTime(new Date());
        tea.setCateId(tea.getCateId());
        tea.setCategory(null);
        teaService.insertTeaAndCategory(tea);
        return "redirect:/grang/selectTeaByPage.do";
    }
    @RequestMapping("/deleteTea")
    public String deleteTea(int id){
        teaService.deleteTeaById(id);
        return "redirect:/grang/selectTeaByPage.do";
    }
    @RequestMapping("/selectTeaById")
    public String selectTeaById(int teaId , Model model){
        Tea tea = teaService.selectTeaAndCategoryById(teaId);
        model.addAttribute("tea",tea);
        List<Category> categories = categoryService.selectAllCategory();
        model .addAttribute("categories",categories);
        return "/updateTea.jsp";
    }
    @RequestMapping("/updateTea")
    public String updateTea(Tea tea){

        tea.setAddTime(new Date());

        tea.setCategory(null);
        teaService.updateTea(tea);
        return "redirect:/grang/selectTeaByPage.do";
    }
    @RequestMapping("/selectAllCategory")
    public String selectAllCategory(Model model){
        List<Category> categories = categoryService.selectAllCategory();
        model.addAttribute("categories",categories);
        return "/categoryList.jsp";
    }
    @RequestMapping("/deleteCategory")
    public String deleteCategory(int categoryId){
        teaService.deleteTeaByCateId(categoryId);
        categoryService.deleteCategoryById(categoryId);
        return "redirect:/grang/selectAllCategory.do";
    }
    @RequestMapping("/selectAllTeaAndCategoryByCategoryName")
    public String selectAllTeaAndCategoryByCategoryName(String categoryName , Model model){
        List<Tea> teas = teaService.selectAllTeaAndCategoryByCategory(categoryName);
        model.addAttribute("teas",teas);
        return "/showCateTea.jsp";
    }
    @RequestMapping("/selectTeaByPage")
    public String selectTeaByPage(Integer curPage,Model model){
        int PageNum = 1;
        if (curPage != null){
            PageNum = curPage;
        }
        PageInfo<Tea> teaPageInfo = teaService.selectTeaByPage(PageNum, 5);
        model.addAttribute("teaPageInfo",teaPageInfo);
        return "/teaList.jsp";
    }
    @RequestMapping("/deleteMany")
    public String deleteMany(int[] ids){
        teaService.deleteMany(ids);
        return "redirect:/grang/selectTeaByPage.do";
    }
    @RequestMapping("/selectAllCategoryInsert")
    public String selectAllCategoryInsert(Model model){
        List<Category> categories = categoryService.selectAllCategory();
        model.addAttribute("categories",categories);
        return "/addTea.jsp";
    }
    @RequestMapping("/abc")
    public String text(String name, int age, Model model, HttpSession session){
        System.out.println(name+age);
        model.addAttribute("a","hello lisi");
        session.setAttribute("b","hello zhangsan");
        return "/a.jsp";
    }
    @RequestMapping("/abcd")
    public String text1(String name, int age, Model model){
        System.out.println(name+age);
        model.addAttribute("a","hello wangw");
        return "/b.jsp";
    }
    @RequestMapping("/selectTeaByPage1")
    public String selectTeaByPage1(  String name, BigDecimal price,Model model){
        List<Tea> teas = teaService.selectByPage(name, price);
        model.addAttribute("teas",teas);
        return "/teaListByNameAndPrice.jsp";
    }


}
